//
//  WFPasswordResetViewController.m
//  WisdomFarm
//
//  Created by sky on 2017/12/21.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFPasswordResetViewController.h"
#import "WFTextFieldTableViewCell.h"
#import "WFCaptchaButton.h"
#import "WFRegisterRequest.h"
#import "WFButtonTableFooterView.h"

@interface WFPasswordResetViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) WFTextFieldTableViewCell *phoneCell;
@property (nonatomic, strong) WFTextFieldTableViewCell *captchaCell;
@property (nonatomic, strong) WFTextFieldTableViewCell *passwordCell;

@property (nonatomic, strong) WFCaptchaButton *captchaButton;

@property (nonatomic, strong) WFButtonTableFooterView *footerView;


@end

@implementation WFPasswordResetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"忘记密码";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.footerView = [WFButtonTableFooterView new];
    [self.view addSubview:self.footerView];
    
    self.tableView.allowsSelection = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;

    [self.footerView.saveButton addTarget:self action:@selector(onSave) forControlEvents:UIControlEventTouchUpInside];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.insets(UIEdgeInsetsZero);
        make.bottom.equalTo(self.footerView.mas_top);
    }];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50);
    }];

    
    _phoneCell = [[WFTextFieldTableViewCell alloc] init];
    _phoneCell.titleWidth = 75;
    _phoneCell.textLabel.text = @"手机号码";
    _phoneCell.textField.placeholder = @"请输入注册手机号码";
    self.captchaButton = [WFCaptchaButton new];
    self.captchaButton.layer.cornerRadius = 4;
    [_phoneCell.contentView addSubview:self.captchaButton];
    [self.captchaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.insets(UIEdgeInsetsMake(7, 0, 7, 15));
        make.width.mas_equalTo(92);
    }];
    [self.captchaButton addTarget:self action:@selector(onCaptcha) forControlEvents:UIControlEventTouchUpInside];
    
    _captchaCell = [[WFTextFieldTableViewCell alloc] init];
    _captchaCell.textLabel.text = @"验证码";
    _captchaCell.textField.placeholder = @"请输入手机验证码";
    _captchaCell.titleWidth = _phoneCell.titleWidth;

    _passwordCell = [[WFTextFieldTableViewCell alloc] init];
    _passwordCell.textLabel.text = @"新密码";
    _passwordCell.textField.placeholder = @"请输入新密码";
    _passwordCell.titleWidth = _phoneCell.titleWidth;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    [self.footerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.insets(UIEdgeInsetsZero);
        make.height.mas_equalTo(50 + self.view.safeAreaInsets.bottom);
    }];
}

- (void)onCaptcha {
    if (self.phoneCell.textField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
        return;
    }
    WFCaptchaRequest *request = [WFCaptchaRequest new];
    request.mobileNum = self.phoneCell.textField.text;
    [request startWithBlock:^(__kindof MZBaseRequest *request, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            [self.captchaButton resetCoolDown];
            return;
        }
    }];
}

- (void)onSave {
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return self.phoneCell;
            break;
        case 1:
            return self.captchaCell;
            break;
        default:
            return self.passwordCell;
            break;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

@end
